package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.competition.entity.Chat;
import com.competition.mapper.ChatMapper;
import com.competition.resp.ResponseResult;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatMapper chatMapper;

    @GetMapping("/getChat")
    public ResponseResult getChat(@RequestParam("openId")String openId,@RequestParam("otherId")String otherId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_user_id",openId);
        queryWrapper.eq("reception_user_id",otherId);
        List<Chat> list = chatMapper.selectList(queryWrapper);
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("send_user_id",otherId);
        queryWrapper1.eq("reception_user_id",openId);
        List<Chat> list1 = chatMapper.selectList(queryWrapper1);
        List<Chat> newData = new ArrayList<>();
        newData.addAll(list1);
        newData.addAll(list);
        return new ResponseResult(200,"获取成功",newData);
    }
    @GetMapping("/sendChat")
    public ResponseResult sendChat(@RequestParam("sendId")String sendid,@RequestParam("reqId")String reqId,@RequestParam("cont")String cont){
        Chat chat = new Chat();
        chat.setChatId(UUIDUtil.getUUID());
        chat.setSendUserId(sendid);
        chat.setReceptionUserId(reqId);
        chat.setChatContent(cont);
        chat.setChatCreateTime(new Date());
        chatMapper.insert(chat);
        return new ResponseResult(200,"添加成功",null);
    }
}
